﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Jp.Nuits.ForwardProxy
{
    public partial class ForwardProxyUI : Form
    {
        #region Attribute
        private readonly ForwardProxy proxy = new ForwardProxy();
        #endregion

        public ForwardProxyUI()
        {
            InitializeComponent();
            LoadProperty();
        }

        private void LoadProperty()
        {
            ListeningPort.Text = Properties.Settings.Default.ListeningPort.ToString();
            RemoteHostName.Text = Properties.Settings.Default.RemoteHostName;
            RemotePort.Text = Properties.Settings.Default.RemotePort.ToString();
            IsAuth.Checked = Properties.Settings.Default.IsAuth;
            UserName.Text = Properties.Settings.Default.UserName;
            PassWord.Text = Properties.Settings.Default.PassWord;
        }

        private bool SaveProperty()
        {
            int listeningPort;
            if (int.TryParse(ListeningPort.Text, out listeningPort))
            {
                Properties.Settings.Default.ListeningPort = listeningPort;
            }
            else
            {
                MessageBox.Show("リスニングポートには半角数字を指定してください。");
                return false;
            }

            Properties.Settings.Default.RemoteHostName = RemoteHostName.Text;
            int remotePort;
            if (int.TryParse(RemotePort.Text, out remotePort))
            {
                Properties.Settings.Default.RemotePort = remotePort;
            }
            else
            {
                MessageBox.Show("ポート番号には半角数字を指定してください。");
                return false;
            }

            Properties.Settings.Default.IsAuth = IsAuth.Checked;
            Properties.Settings.Default.UserName = UserName.Text;
            Properties.Settings.Default.PassWord = PassWord.Text;

            Properties.Settings.Default.Save();

            return true;
        }

        private void IsAuth_CheckedChanged(object sender, EventArgs e)
        {
            if (IsAuth.Checked)
            {
                UserName.Enabled = true;
                PassWord.Enabled = true;
            }
            else
            {
                UserName.Enabled = false;
                PassWord.Enabled = false;
            }
        }

        private void RunButton_Click(object sender, EventArgs e)
        {
            if (SaveProperty())
            {
                proxy.ListeningPort = int.Parse(ListeningPort.Text);
                proxy.RemoteHostName = RemoteHostName.Text;
                proxy.RemotePort = int.Parse(RemotePort.Text);
                proxy.IsAuth = IsAuth.Checked;
                proxy.UserName = UserName.Text;
                proxy.PassWord = PassWord.Text;
                proxy.Run();

                RunButton.Enabled = false;
                StopButton.Enabled = true;
            }
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            proxy.Stop();

            RunButton.Enabled = true;
            StopButton.Enabled = false;
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            SaveProperty();
        }
    }
}
